package com.hp.bon.sgw.ctrl;

import com.hp.bon.sgw.domain.CapabilityInfo;
import com.hp.bon.sgw.domain.Node;
import com.hp.bon.sgw.domain.OpenedCapability;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * 获取Node数据，从数据库操作
 * 
 * @author wuzhih
 * 
 */
public class NodeDataFetcher {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(NodeDataFetcher.class);
	private HibernateTemplate hibernateTemplate;

 
	/**
	 * 得到所有Node
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public Map<Integer, Node> getNodeMap() {
		Map<Integer, Node> map = new HashMap<Integer, Node>();
		List<Node> list = hibernateTemplate.find("select c from Node c ");
		for (Node n : list) {
			map.put(n.getId(), n);
		}
		return map;
	}

	/**
	 * 得到所有能力信息
	 * 
	 * @param sessionFact
	 */
	@Transactional(readOnly = true)
	public List<CapabilityInfo> getCapablityInfos() {
		try {
			return hibernateTemplate.loadAll(CapabilityInfo.class);
		} catch (Exception e) {
			logger.warn("get sr info err " + e.toString());
			return null;
		}
	}

	/**
	 * 得到网元，用户能力授权信息
	 * 
	 * @param sessionFact
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<OpenedCapability> getOpenedCapabilityList() {
		try {
			return hibernateTemplate.find("select c from OpenedCapability c");
		} catch (Exception e) {
			logger.warn("get OpenedCapability info err " + e.toString());
			return null;
		}
	}

	@Autowired
	@Qualifier("sessionFactory")
	public void setSessionFact(SessionFactory sessionFact) {
		hibernateTemplate = new HibernateTemplate(sessionFact);
	}
}